/*
 * vertex.java
 *
 * Created on March 11, 2011, 2:56 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ifs.RnD.Graph;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import ifs.RnD.svnRead.Tree;
import ifs.RnD.svnRead.DisplayRepositoryTree;
import ifs.RnD.svnRead.Node;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tmatesoft.svn.core.SVNException;

/**
 *
 * @author CHJSLK
 */
public class Vertex extends JLabel {

    boolean Final;
    /**
     *
     */
    public int number;
    private String value, repoUrl;
    /**
     *
     */
    public Edge empty = null;  //to handel empty string *****************
    Vertex parent = null;
    Edge e = null;
    Tree tree;
    DisplayRepositoryTree disRepoTree;
    RepositoryGraph canvas;
    String type;
    Iterator iterator1;
    Iterator iterator2;
    Iterator iterator3;
    Vertex v;
    JLayeredPane jlp;
    JLabel jl;

    /** Creates a new instance of vertex
     * @param text
     * @param url
     * @param canvs
     * @param nodeType
     * @param dr
     */
    public Vertex(String text, String url, final RepositoryGraph canvs, DisplayRepositoryTree dr, String nodeType) {
        if (disRepoTree == null) {
            disRepoTree = dr;
            canvas = canvs;
        }
        type = nodeType;
        String revisionUrl = null;
        String nType = null;

        value = text;
        repoUrl = url;

        setFont(new Font("tahoma", 0, 12));
        setText(text);
        setSize(100, 50);

        if (type.equals("DIR")) {
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/green-light.png")));
        } else if (type.equals("FILE")) {
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/yellow-light.png")));
        } else if (type.equals("midle")) {
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/purple-circle.png")));
        } else if (type.equals("EXTERNAL")) {
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/blue-light.png")));
        } else {
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/purple-circle.png")));
        }



        setHorizontalTextPosition(Vertex.HORIZONTAL);

        List list1 = (List) disRepoTree.directTree.toList();
        iterator1 = list1.iterator();

        while (iterator1.hasNext()) {
            Node node = (Node) iterator1.next();
            String name = node.getData();
            if (name.equals(text)) {
                setToolTipText(node.getData());
            }
        }

        addMouseListener(new java.awt.event.MouseAdapter() {

            /**
             * handles mouse click events.
             * @param
             * @exception Any exception
             * @return
             */
            public void mouseClicked(MouseEvent evt) {
                try {
                    mouseSingleClicked(evt);
                } catch (SVNException ex) {
                    Logger.getLogger(Vertex.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            /**
             * changes label appearance and mouse pointer when mouse enter to a label.
             * @param
             * @exception Any exception
             * @return
             */
            public void mouseEntered(MouseEvent e) {

                Cursor cursor = Cursor.getDefaultCursor();
                //change cursor appearance to HAND_CURSOR when the mouse pointed on images
                cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
                if (type.equals("DIR")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/green-dark.png")));
                } else if (type.equals("FILE")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/yellow-light.png")));
                    cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
                } else if (type.equals("midle")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/gray-central.png")));
                } else if (type.equals("EXTERNAL")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/blue-dark.png")));
                } else {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/gray-central.png")));
                }
                setFont(new Font("tahoma", 1, 14));
                setCursor(cursor);
            }

            /**
             * reset label appearance and mouse pointer to the default when mouse exit from a label.
             * @param
             * @exception Any exception
             * @return
             */
            public void mouseExited(MouseEvent e) {
                setFont(new Font("tahoma", 0, 12));
                canvs.getLable().setText("");
                if (type.equals("DIR")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/green-light.png")));
                } else if (type.equals("FILE")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/yellow-light.png")));
                } else if (type.equals("midle")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/purple-circle.png")));
                } else if (type.equals("EXTERNAL")) {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/blue-light.png")));
                } else {
                    setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/purple-circle.png")));
                }
            }
        });
        {
            List list2 = (List) disRepoTree.directTree.toList();
            iterator2 = list2.iterator();
            while (iterator2.hasNext()) {
                Node nodeTest = (Node) iterator2.next();
                String name = nodeTest.getData();

                if (name.equals(value)) {
                    revisionUrl = nodeTest.getSvnURL();
                    nType = nodeTest.getType();
                }
            }
        }
        addMouseListener(new NodeMenuListener(revisionUrl, nType, disRepoTree));
    }

    /**
     * draw a oval on the label
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (Final) {
            g.drawOval(5, 5, 40, 40);
        }
    }

    /**
     *
     */
    public void select() {
    }

    /**
     * sets the label nodeUrl
     * @param text
     */
    public void setLableValue(String text) {
        value = text;
        setText(text);
    }

    /**
     *
     */
    public void unselect() {
    }

    /**
     * identifies the selected labe and gets its nodeUrl
     * @param evt
     * @return
     */
    public String getSelectedNode(MouseEvent evt) {
        Vertex ver = (Vertex) evt.getSource();
        return ver.value;
    }

     /**
      * handles events when double click on a label.
      * @param evt
      * @throws SVNException
      */
     public void mouseSingleClicked(MouseEvent evt) throws SVNException {
          try {
               canvas.getLable().setText("<html>Reading  the <br>Repository ....</html> ");
          } finally {
               Vertex ver = (Vertex) evt.getSource();
               List list = (List) disRepoTree.directTree.toList();
               iterator2 = list.iterator();
               while (iterator2.hasNext()) {
                    Node node = (Node) iterator2.next();
                    String nodeUrl = node.getSvnURL();
                    if (nodeUrl.equals(repoUrl)) {
                         canvas.setForwardNode(node);
                         if (node.getNumberOfChildren() == 0 && (node.getType().equalsIgnoreCase("DIR"))) {
                              if (disRepoTree.readExternals(node.getSvnURL())) {
                                   canvas.drawGraph(node);
                                   canvas.getLable().setText("");
                              } else {
                                   disRepoTree.cerateRepoToURL(node.getSvnURL());
                                   Node root = disRepoTree.readRepository(disRepoTree.getRepository(), "");
                                   canvas.getLable().setText("");
                                   if (node.getNumberOfChildren() == 0) {
                                        canvas.getLable().setText("<html> This Directory<br>is empty ....</html>");
                                   } else {
                                        canvas.drawGraph(node);
                                        System.out.println("");
                                   }
                              }

                         } else if (node.getType().equalsIgnoreCase("FILE")) {
                              canvas.getLable().setText("<html>This is a File<html>");

                         } else if (node.getType().equalsIgnoreCase("EXTERNAL")) {
                              String extUrl = getUrlStrForParsing(node.getSvnURL());
                              disRepoTree.processLink(extUrl, disRepoTree);
                              canvas.getLable().setText("");
                         } else if (node.getParent() == null) {
                              canvas.getLable().setText("<html>This is the<br>Repository Root</html>");
                         } else if (canvas.rootNode == node) {
                              canvas.drawGraph(node.getParent());
                              canvas.setVisible(true);
                              canvas.getLable().setText("");
                         } else {
                              canvas.drawGraph(node);
                              canvas.setVisible(true);
                              canvas.getLable().setText("");
                         }
                    }
                }
            }
        }
    

    private String getUrlStrForParsing(String extUrl) {
        try {
            if (extUrl.startsWith("http:") || extUrl.startsWith("https:") || extUrl.startsWith("svn:") || extUrl.startsWith("file:")) {
                //Assume extUrl is in correct format.
                return extUrl;
            } else if (extUrl.startsWith("^")) {
                extUrl = extUrl.substring(1);
                return disRepoTree.getRootURL().concat(extUrl);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * search nodes and read them
     * @param s
     * @throws SVNException
     */
    public void search(String s) throws SVNException {
        boolean b = false;
        List list = (List) disRepoTree.directTree.toList();
        iterator3 = list.iterator();
        while (iterator3.hasNext()) {
            Node node = (Node) iterator3.next();
            String name = node.getData();
            if (name.equals(s)) {
                b = true;
                canvas.setForwardNode(node);
                if (node.getNumberOfChildren() == 0 && node.getType().equalsIgnoreCase("DIR")) {
                    disRepoTree.cerateRepoToURL(node.getSvnURL());
                    Node root = disRepoTree.readRepository(disRepoTree.getRepository(), "");
                    if (node.getNumberOfChildren() == 0) {
                        JOptionPane.showMessageDialog(labelFor, "This directory is empty");
                    } else {
                        canvas.drawGraph(node);
                    }
                } else if (node.getType().equalsIgnoreCase("FILE")) {
                    JOptionPane.showMessageDialog(labelFor, "This is not a directory");
                } else if (node.getParent() == null) {
                    JOptionPane.showMessageDialog(labelFor, "This is the root");
                } else if (canvas.rootNode == node) {
                    canvas.drawGraph(node.getParent());
                    canvas.setVisible(true);
                } else {
                    canvas.drawGraph(node);
                    canvas.setVisible(true);
                }

            }
        }
        if (b == false && s != null && !s.equals("")) {
            JOptionPane.showMessageDialog(labelFor, "\"" + s + "\"" + " not found", "Error", 0);
        }
    }
}
